﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Distribution.Service;
using DistributionApplication.Controls.Events;
using Entities;

namespace DistributionApplication.Controls
{
    public partial class HomeGrid : UserControl
    {
        private Int32 m_SelectedId = Int32.MinValue;

        public HomeGrid()
        {
            InitializeComponent();
        }

        public void ConfigureGrid()
        {
            grdDistribution.RowHeadersVisible = false;

            #region Set grid styles
            DataGridTableStyle _GridTableStyle = new DataGridTableStyle();
            DataGridTextBoxColumn _DGCol;

            #region PoleId

            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "test";
            _DGCol.HeaderText = "Pole";
            _DGCol.Width = 0;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);

            #endregion

            #region PoleId

            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "PoleId";
            _DGCol.HeaderText = "Pole";
            _DGCol.Width = 50;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            
            #endregion

            #region Number

            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "Number";
            _DGCol.HeaderText = "Number";
            _DGCol.NullText = String.Empty;
            _DGCol.Width = 60;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            
            #endregion

            #region Meter
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "Meter";
            _DGCol.HeaderText = "Meter";
            _DGCol.NullText = "0";
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion

            #region Number
            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "Time";
            _DGCol.HeaderText = "Time";
            _DGCol.Width = 70;
            _DGCol.NullText = String.Empty;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            #endregion
            
            grdDistribution.TableStyles.Add(_GridTableStyle);
            ReloadGrid();

            #endregion
        }

        public void ReloadGrid()
        {
            DataTable poles = LPole.GetPoleDetails();

            grdDistribution.DataSource = poles;

            if (poles.Rows.Count > 0)
                grdDistribution.Select(poles.Rows.Count - 1);
        }

        public Int32 GetSelectedInspection()
        {
            if (grdDistribution.CurrentRowIndex >= 0 && grdDistribution.IsSelected(grdDistribution.CurrentRowIndex))
                m_SelectedId = (Int32)grdDistribution[grdDistribution.CurrentRowIndex, 1];
            else
                m_SelectedId = Int32.MinValue;

            return m_SelectedId;
        }

        public EPole GetLastInspection()
        {
            EPole pole = null;

            if (grdDistribution.VisibleRowCount > 0) 
            {
                pole = new EPole();
                pole.Id = (Int32)grdDistribution[grdDistribution.VisibleRowCount - 1, 1];
            }
        
            return pole;
        }

        private void grdInspections_CurrentCellChanged(object sender, EventArgs e)
        {
            if (grdDistribution.CurrentRowIndex >= 0)
            {
                Rectangle r = grdDistribution.GetCellBounds(grdDistribution.CurrentRowIndex, 4);
                grdDistribution.Select(grdDistribution.CurrentRowIndex);
                menuGrid.Show(grdDistribution, new Point(r.X, r.Y));
            }
        }

        #region HomeGridEvents

        public event EventHandler<GridEventArgs> ClickedAddMeter;
        public event EventHandler<GridEventArgs> ClickedMeters;
        public event EventHandler<GridEventArgs> ClickedAddTransformer;
        public event EventHandler<GridEventArgs> ClickedTransformers;
        public event EventHandler<GridEventArgs> ClickedDelete;
        public event EventHandler<GridEventArgs> ClickedAddRecloser;
        public event EventHandler<GridEventArgs> ClickedReclosers;
        public event EventHandler<GridEventArgs> ClickedAddSectionalizer;
        public event EventHandler<GridEventArgs> ClickedSectionalizers;
        public event EventHandler<GridEventArgs> ClickedEdit;
        public event EventHandler<GridEventArgs> ClickedDuplicate;

        private void OnClickedMeters(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedMeters != null)
            {
                ClickedMeters(o, e);
            }
        }

        private void OnClickedAddMeter(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddMeter != null)
            {
                ClickedAddMeter(o, e);
            }
        }

        private void OnClickedTransformers(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedTransformers != null)
            {
                ClickedTransformers(o, e);
            }
        }

        private void OnClickedAddTransformer(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddTransformer != null)
            {
                ClickedAddTransformer(o, e);
            }
        }

        private void OnClickedReclosers(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedReclosers != null)
            {
                ClickedReclosers(o, e);
            }
        }

        private void OnClickedAddRecloser(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddRecloser != null)
            {
                ClickedAddRecloser(o, e);
            }
        }
        private void OnClickedSectionalizers(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedSectionalizers != null)
            {
                ClickedSectionalizers(o, e);
            }
        }

        private void OnClickedAddSectionalizer(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddSectionalizer != null)
            {
                ClickedAddSectionalizer(o, e);
            }
        }

        private void OnClickedDelete(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }

        private void OnClickedEdit(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }

        private void OnClickedDuplicate(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDuplicate != null)
            {
                ClickedDuplicate(o, e);
            }
        }

        private void mitDuplicate_Click(object sender, EventArgs e)
        {
            OnClickedDuplicate(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitEdit_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitAddMeter_Click(object sender, EventArgs e)
        {
            OnClickedAddMeter(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitMeters_Click(object sender, EventArgs e)
        {
            OnClickedMeters(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitAddTransformer_Click(object sender, EventArgs e)
        {
            OnClickedAddTransformer(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitTransformersList_Click(object sender, EventArgs e)
        {
            OnClickedTransformers(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitDelete_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitAddRecloser_Click(object sender, EventArgs e)
        {
            OnClickedAddRecloser(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitReclosersList_Click(object sender, EventArgs e)
        {
            OnClickedReclosers(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitAddSectionalizer_Click(object sender, EventArgs e)
        {
            OnClickedAddSectionalizer(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitSectionlizersList_Click(object sender, EventArgs e)
        {
            OnClickedSectionalizers(sender, new GridEventArgs(GetSelectedInspection()));
        }

        #endregion

    }
}
